// 这个是用户自己的数据 和shop数据是不一样的

import { reactive } from 'vue';
import { Address, addressInit } from '../vendor/address/Address';
import { Order } from '../vendor/order_food/order';

export type State = {
	id: number,
	token: string;
	p_id: number;
	nickname: string;
	avatar: string;
	ali_name: string;
	ali_phone: string;
	store_phone: string;
	store_address: string;
	store_location: string;
	store_longitude: string;
	store_latitude: string;
	store_fee_rate: number; // 费率
	store_td_cash: number; // 今日收款数
	store_td_orders: number; // 今日订单数
	store_can_withdrawal: number; // 可提现数

	addresses: Array<Address>;
	address: Address;
	editAddress: Address;

	busOrders: Array<Order>; // 卖方订单
	busOrderPage: number  // 当前订单页
	
	userOrders: Array<Order>; // 买方订单
	userOrderPage: number // 当前订单页

	successOrder: Order; // 支付成功订单
	
	role: number; // 用户当前角色 1=用户 2=商家
	
	store_mini_code: string // 小程序二维码
	store_mini_price: number // 最低配送金额
};

export const ROLE_MER = 2; // 商家
export const ROLE_USER = 1; // 用户

export const state = reactive({
	//token: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjQ4NzA0NjkyMzcsImlhdCI6MTcxNjg2OTIzNywicGF5bG9hZCI6IntcImlkXCI6MSxcIm9wZW5faWRcIjpcIm91MVFWN2FXaDYtQ2xtUFR2aVhBcXRhUEppYTRcIixcInBfaWRcIjoxfSJ9.GPAQTlEMq9sLqE0FLRGHIi5_uqGnJTVewSmwnkAar3g',
	token:'',
	//token: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjQ4NzA4OTYzMjcsImlhdCI6MTcxNzI5NjMyNywicGF5bG9hZCI6IntcImlkXCI6MSxcIm9wZW5faWRcIjpcIm91MVFWN1dZTVpHZU1JT0Mxdm5Rek16cnV4OVFcIixcInBfaWRcIjoyfSJ9.QyCFUXpkDkz79eWHe0GbWIX6DaRFTscUNdLWDVEpdZ4",
	id: 1,
	p_id: 1,
	nickname: '',
	avatar: '',
	ali_name: '',
	ali_phone: '',
	store_phone: '',
	store_address: '',
	store_location: '',
	store_longitude: '',
	store_latitude: '',
	store_fee_rate: 0, // 费率
	store_td_cash: 0, // 今日收款数
	store_td_orders: 0, // 今日订单数
	store_can_withdrawal: 0, // 可提现数
	store_mini_code: '', // 小程序二维码 
	store_mini_price: 0, // 最低配送金额
	addresses: [],
	address: addressInit,
	editAddress: addressInit,
	busOrders: [],
	userOrders: [],
	role: ROLE_USER,
	busOrderPage:1,
	userOrderPage: 1
} as State);

export const setUser = ({
	id,
	token,
	p_id,
	nickname,
	avatar,
	ali_name,
	ali_phone,
	store_phone,
	store_address,
	store_location,
	store_longitude,
	store_latitude,
	store_fee_rate,
	store_td_cash,
	store_td_orders,
	store_can_withdrawal,
	store_mini_code,
	store_mini_price
}) => {
	state.store_td_cash = store_td_cash;
	state.store_td_orders = store_td_orders;
	state.store_can_withdrawal = store_can_withdrawal;

	if (id) {
		state.id = id;
	}

	if (token) {
		state.token = token;
		uni.setStorageSync("token",state.token)
	}

	if (p_id) {
		state.p_id = p_id;
	}

	if (nickname) {
		state.nickname = nickname;
	}

	if (avatar) {
		state.avatar = avatar;
	}

	if (ali_name) {
		state.ali_name = ali_name;
	}

	if (ali_phone) {
		state.ali_phone = ali_phone;
	}

	if (store_phone) {
		state.store_phone = store_phone;
	}

	if (store_address) {
		state.store_address = store_address;
	}

	if (store_location) {
		state.store_location = store_location;
	}

	if (store_longitude) {
		state.store_longitude = store_longitude;
	}

	if (store_latitude) {
		state.store_latitude = store_latitude;
	}

	if (store_fee_rate) {
		state.store_fee_rate = store_fee_rate;
	}
	
	if(store_mini_code) {
		state.store_mini_code = store_mini_code
	}
	
	state.store_mini_price  = store_mini_price
};


export const setPId = (id:number)=>{
	uni.setStorageSync("pid",id)
}

export const getPId = ()=>{
	return uni.getStorageSync("pid")
}

export const getToken = ()=>{
	if(state.token) return state.token
	else {
		state.token =  uni.getStorageSync("token")
		if(state.token == undefined ){
			state.token = ""
		}
		return state.token
	}
}

export const setAddresses = (addresses: Array<Address>) => {
	state.addresses.length = 0;
	addresses.forEach((item) => {
		state.addresses.push(item);
	});
};

export const setAddress = (address: Address) => {
	state.address = address;
};

export const setEditAddress = (address: Address) => {
	state.editAddress = address;
};

export const setAddBusOrder = (order: Order) => {
	state.busOrders.unshift(order);
};

export const setAddUserOrder = (order: Order) => {
	state.userOrders.unshift(order);
};

export const setSuccessOrder = (order: Order) => {
	state.successOrder = order;
};
export const setChangeRole = () => {
	if (state.role == ROLE_USER) {
		state.role = ROLE_MER;
	} else {
		state.role = ROLE_USER;
	}
};
